package com.example.controller;


import com.example.entity.Users;
import com.example.service.UsersService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;

@Controller
public class UsersCtrl {
@Resource
    UsersService usersService;

@RequestMapping(value = "/users",method = {RequestMethod.GET,RequestMethod.POST})
public String findAllGoods(Model model){
    ArrayList<Users> userslist = usersService.queryAll();
    model.addAttribute("userslist",userslist);
    return "userslist";
}

    @GetMapping("/users/{id}")
    public String getUserById(@PathVariable("id") int id, Model model) {
    Users users =usersService.findById(id);
    model.addAttribute("users",users);
    return "usersEdit";
    }

    @PostMapping("/editUsers")
    public String addUsers(Users users,Model model){
        System.out.println(users);
        int num = usersService.update(users);
        model.addAttribute("msg","商品信息更新成功！");
        return "usersEdit";
}

    @GetMapping("/delUsers/{id}")
    public String delGoods(@PathVariable("id") int id, Model model){
        int num =usersService.deleteByID(id);
        model.addAttribute("msg","删除成功");
        return "forward:/users";
    }




    @GetMapping("/usersadd")
    public  String insertGoods(Users users, Model model){
        ArrayList<Users> usersArrayList = usersService.queryAll();
        return "usersadd";
    }

    @PostMapping("/user2")
    public  String addGood(Users users,Model model){
        System.out.println(users);
        int num2 = usersService.insert(users);
        return "redirect:users";
    }

}
